package io.renren.task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.entity.WxuserPersonal;
import io.renren.service.WxuserPersonalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.binarywang.demo.spring.service.WeixinService;


import io.renren.entity.Currentcrossborderalarm;
import io.renren.entity.Personneldisappearalarm;
import io.renren.entity.WeixinUser;
import io.renren.service.CurrentcrossborderalarmService;
import io.renren.service.PersonneldisappearalarmService;
import io.renren.service.WeixinUserService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;


public class TestJob1 {
	@Autowired
	private PersonneldisappearalarmService personneldisappearalarmService;
	@Autowired
	private CurrentcrossborderalarmService currentcrossborderalarmService;
	@Autowired
	private WeixinService weixinService;
	@Autowired
	private WeixinUserService weixinUserService;
	@Autowired
	private WxuserPersonalService wxuserPersonalService;
	private Logger logger = LoggerFactory.getLogger(getClass());
	 public void doSomething() {
		 Map<String, Object> usermap = new HashMap<>();
		 //班主
		 usermap.put("roleid",2);
		 //正常状态
		 usermap.put("stateid",1);
		 //上班
		 usermap.put("gowork",1);
		 List<WeixinUser> userlist=weixinUserService.queryList(usermap);
		 
		 Map<String, Object> personneldisappearalarmmap = new HashMap<>();
		 personneldisappearalarmmap.put("issend", 0);
		 List<Personneldisappearalarm> disappearalarmlist=personneldisappearalarmService.queryList(personneldisappearalarmmap);
		
		 Map<String, Object> currentcrossborderalarmmmap = new HashMap<>();
		 currentcrossborderalarmmmap.put("issend", 0);
		 List<Currentcrossborderalarm> currentcrossborderalarmlist=currentcrossborderalarmService.queryList(currentcrossborderalarmmmap);

	
		 for(int i=0;i<userlist.size();i++){
			 String content="";
			 content+="****越界列表****\n";
			 for(Currentcrossborderalarm c:currentcrossborderalarmlist){
				 WxuserPersonal wxuserPersonal=wxuserPersonalService.queryObject(c.getIdPersonnel());
				 if(wxuserPersonal.getIdWeixinUser().equals(userlist.get(i).getId()))
				 content+=""+c.getNamePersonnel()+"-"+c.getCodeTag()+"在"+c.getNameStation()+"越界\n";
				 Currentcrossborderalarm currentcrossborderalarm=currentcrossborderalarmService.queryObject(c.getId());
				 currentcrossborderalarm.setIssend(1);
				 currentcrossborderalarmService.update(currentcrossborderalarm);
			 }
			 content+="****离岗列表****\n";
			 for(Personneldisappearalarm d:disappearalarmlist){
				 WxuserPersonal wxuserPersonal=wxuserPersonalService.queryObject(d.getIdPersonnel());
				 if(wxuserPersonal.getIdWeixinUser().equals(userlist.get(i).getId()))
				 content+=""+d.getNamePersonnel()+"-"+d.getCodeTag()+"在"+d.getNameStation()+"离岗\n";
				 Personneldisappearalarm personneldisappearalarm=personneldisappearalarmService.queryObject(d.getId());
				 personneldisappearalarm.setIssend(1);
				 personneldisappearalarmService.update(personneldisappearalarm);
			 }

			 WxMpKefuMessage wxMpKefuMessage=new WxMpKefuMessage();
			 wxMpKefuMessage.setToUser(userlist.get(i).getOpenid());
			 wxMpKefuMessage.setMsgType(WxConsts.XML_MSG_TEXT);
			 wxMpKefuMessage.setContent(content);
			 try {
				 if((currentcrossborderalarmlist!=null && !currentcrossborderalarmlist.isEmpty())||(disappearalarmlist!=null && !disappearalarmlist.isEmpty())){
				if(weixinService.getKefuService().sendKefuMessage(wxMpKefuMessage)){
					logger.info("报警信息发送至用户："+userlist.get(i).getOpenid());
				
				}
				}
			} catch (WxErrorException e) {
				
				e.printStackTrace();
			}
			 
		 }
		 
	    }

}
